import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router,ActivatedRoute } from "@angular/router"; //路由引入
import {config} from '../../shared/smartadmin.config';

declare var $: any;
declare var echarts: any;
@Component({
  selector: 'sa-sensor',
  templateUrl: './sensor.component.html',
})
export class SensorComponent implements OnInit {
	tableData:any[] = []; //table数据
	selected:any[] = []; //row选择的数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	searchKey:string = ''; //搜索keyword
	status:string = '0';
	selectedCompanyName:string = '';
	selectedCompanyId:string = '';
	unnormalSensor:any[] = [];
	userChannels:any[] = [];
	chart:any;
	deviceId:string = '';
	resetReason:string = '';
	constructor(private jsonApiService: JsonApiService, private router: Router,private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage? queryParams.nowPage:1;
			this.status = queryParams.status?queryParams.status:'0';
		});
	}

	ngOnInit() {
		this.load();		
	}
	ngOnDestroy(){
		
	}
	//AD通道绘图
	ad_map(row){
		let url:string = '',barX:any[] = [],legendData:any[]=[],seriesData:any[] = [],temAD:any[]=[];
		this.chart = echarts.init(document.getElementById('ad_map'));
		for(let ad of this.userChannels){//在用的通道处理
			if(row.damageChannels.includes(ad)){
				var obj = {
					name:'aCH'+ad,
					type:"line",
					itemStyle :{
						color:'#c23531'
					},
					data:[]
				}
			}else{
				var obj = {
					name:'aCH'+ad,
					type:"line",
					itemStyle :{
						color:''
					},
					data:[]
				}
			}
			legendData.push('aCH'+ad);
			seriesData.push(obj);
		}
		let option = {
			color:['#2f4554', '#61a0a8', '#d48265', '#91c7ae','#749f83',  '#ca8622', '#bda29a','#6e7074', '#546570', '#c4ccd3'],
			title:{
				text:'正常通道和损坏通道统计',
				top:0,
				textStyle:{
					fontSize:14,
				}
			},
		    legend: {
		    	top:'24',
		        data:legendData
		   	},
		    tooltip:{
		        trigger:'axis',
		        axisPointer:{type:'cross'}
		    },
		    grid:{
		    		top:'72',
		        left:'22',
		        right:'32',
		        bottom:'22',
		        containLabel: true
		    },
		    xAxis:[{
	            type:'category',
	            data:barX,
	            axisTick: {alignWithLabel: true}
	       	}],
		    yAxis:[{type:'value'}],
		    series:seriesData,
		};
		this.chart.clear();
		this.chart.setOption(option);
		let request = "deviceId="+row.deviceId+"&startTime="+row.checkStartDate+"&endTime="+row.checkEndDate+"&showModify=false";
		this.jsonApiService.http('stats/draw.html', request).subscribe(data => {
			let result = data.data.length;
			if(result > 0){
				for(let item of data.data){
					barX.push(item.uploadDate);
					for(let lis in item){
						for(let ad of seriesData){
							if(lis === ad.name){
								ad.data.push(item[lis]);
							}
						}	
					}	
				}
				this.chart.setOption({
					legend:{data:legendData},
					xAxis:[{data: barX}],
	                series:seriesData
	            });
	             $(window).resize(this.chart.resize);
			}else{
				this.jsonApiService.message('通道数据为空')
			}
		})
		
		
	}
	//异常传感器详情查看
	sensorDetail(row,val){
		if(this.chart){
			this.chart.clear();
		}
		let reqdata = 'deviceId='+row.deviceId;
		this.userChannels = row.userChannels;
		this.jsonApiService.http("sensormonitor/getSensorStatusByDeviceId.html", reqdata).subscribe(data => {
			if(data.success){
				this.unnormalSensor = data.data;
				val.show();
			}else{
				this.jsonApiService.message(data.message)
			}
		})
	}
	cancelsensor(val){
		this.resetReason = '';
		val.hide();
	}
	//重置传感器原因
	resetReasonChange(val){
		this.resetReason = val;
	}
	//重置传感器
	resetSensor(row,val){
		val.show();
		this.deviceId = row.deviceId;
	}
	resetSubmit(val){
		if(!this.resetReason){
			this.jsonApiService.message('重置原因不能为空');
			return;
		}
		let reqdata = 'deviceId='+this.deviceId+'&logType='+this.resetReason;
		this.jsonApiService.http("sensormonitor/resetForSensorInfo.html", reqdata).subscribe(data => {
			this.jsonApiService.message(data.message);
			if(data.success){
				this.load();
				this.resetReason = '';
				val.hide();
			}
		})
	}
	cancelReset(val){
		val.hide();
	}
	//删除
	delSensor(row){
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(()=>{
			let reqdata = 'deviceId='+row.deviceId;
			this.jsonApiService.http("sensormonitor/deleteForSensorInfo.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				this.load();
			})
		})
	}
	//表格搜索
	table_search() {
		this.curPage = 1;
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
    //刷新
    refresh(){
    	this.load();
    }
	//初始化加载list数据
	load() {
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				searchKey:this.searchKey,
				companyId:this.selectedCompanyId,
				status:this.status
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http("sensormonitor/sensorInfoForPage.html", reqdata).subscribe(data => {
			if(data.isSuccess){
				this.tableData = data.exhibitDatas;
				this.totalCount = data.recordCount;
			}
		})
	}
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}
	//公司选中回调
	companySelected(data){
		this.selectedCompanyId = data.companyId;
		this.selectedCompanyName = data.companyName;
	}
}
